//游戏操作
function GCDown(message){
	switch(message.area){
		case "board":{
			if(_board[message.x][message.y]!=null){
				if(_board[message.x][message.y].id ==_GDNowChess.id){
					_GDselect = new GDposition(message.x,message.y);
					// 可移动之条件
					if(_board[message.x][message.y].team == 0 && CSgetState("可移动",_board[message.x][message.y].id)){
						GUIHideNowAction();
						_GDway.push(new GDWay(message.x,message.y,0));
						_GDway[0].sprite = new ControlSprite(_GDway[0].no);
						g_GBTouchLayer.addChild(_GDway[0].sprite);
					}
				}
				else{
					_GDWindow = "状态栏";
					_GDStateChess = _board[message.x][message.y];
					GUIShowStateWindow();
				}
			}
			break;
		}
		case "skill":{
			var skill = (message.x+(message.y*2));
			if(CSgetState("可施放技能"+skill,_GDNowChess.id)){
				_GDNowSkill = skill;
				_GUISkill[_GDNowSkill].setLocalZOrder(100);
				GUIHideNowAction();
			}
			break;
		}
		case "状态栏":{
			_GDNowMenu = "";
			GUIShowSkillIcon(_GDNowChess);
			_GDWindow = "状态栏";
			_GDStateChess = _GDNowChess;
			GUIShowStateWindow();
			break;
		}
		case "选单":{
			switch(message.x+message.y*2){
				case 0:{
					if((!_GDThisTurnNoMoveList.length)||_GDMoveDone){
						break;
					}
					_GDNowMenu = "";
					GUIHideNowAction();
					_GEEventList.push(new GEEvent("延迟",_GDNowChess.id));
					GEAskForResponse();
					break;
				}
				case 1:{
					_GDNowMenu = "";
					GUIHideNowAction();
					_GEEventList.push(new GEEvent("结束",_GDNowChess.id));
					GEAskForResponse();
					break;
				}
				case 2:{
					_GDNowMenu = "";
					GUIShowSkillIcon(_GDNowChess);
					_GDWindow = "状态栏";
					_GDStateChess = _GDNowChess;
					GUIShowStateWindow();
					break;
				}
				case 3:{
					g_GBTouchLayer.removeAllChildren(true);
					cc.director.runScene(new cc.TransitionPageTurn(1.2, new ChildMenuScene(),true));
					break;
				}
			}
			break;
		}
		case "退出状态栏":{
			_GDWindow = "";
			g_GBTouchLayer.removeChild(_GUIState);
			break;
		}
		case "退出选单":{
			_GDNowMenu = "";
			GUIShowSkillIcon(_GDNowChess);
			break;
		}
		case "pic":{
			
		}
		case "yes":{

		}
		case "no":{

		}
		case "close":{

		}
	}
};

function GCMove(message){
	// 拖动
	if(_GDNowSkill!=null){
		// 拖动技能
		_GUISkill[_GDNowSkill].setPosition(message.locationX+50,message.locationY-120);
	}
	switch(message.area){
	case "other":{
		if(_GDNowSkillPosition!=null){
			_GDNowSkillPosition = null;
			g_GBTouchLayer.removeChild(_GUISprite);
		}
		break;
	}
	case "board":{
		if(!_GDway.length){
			if(_GDNowSkill!=null){
				if(_GDNowSkillPosition!=null){
					if((_GDNowSkillPosition.x!=message.x||_GDNowSkillPosition.y!=message.y )&& _GDNowChess.skill[_GDNowSkill].target!="self"){
						g_GBTouchLayer.removeChild(_GUISprite);
						if(Target(new GDposition(message.x,message.y),_GDNowChess.skill[_GDNowSkill].target)&&Range(_GDNowChess.position,new GDposition(message.x,message.y),_GDNowChess.skill[_GDNowSkill].range)){
							_GDNowSkillPosition =  new GDposition(message.x,message.y);
							GUIShowSkillTarget(message);
						}
						else{
							_GDNowSkillPosition = null;
						}
					}
				}
				else if(_GDNowChess.skill[_GDNowSkill].target=="self"){
					_GDNowSkillPosition =  new GDposition(_GDNowChess.position.x,_GDNowChess.position.y);
					GUIShowSkillTarget(_GDNowChess.position);
				}
				else if(Target(new GDposition(message.x,message.y),_GDNowChess.skill[_GDNowSkill].target)){
					if(Range(_GDNowChess.position,new GDposition(message.x,message.y),_GDNowChess.skill[_GDNowSkill].range)){
							_GDNowSkillPosition =  new GDposition(message.x,message.y);
							GUIShowSkillTarget(message);
					}
				}
			}
		}
		// 移动
		else if(!_GDMoveDone){
			// 判断指向新格
			if(!(_GDway[_GDway.length-1].x == message.x&&_GDway[_GDway.length-1].y == message.y)){
				// 判断目标可攻击
				if(Range(new GDposition(_GDway[0].x,_GDway[0].y),new GDposition(message.x,message.y),CSgetState("射程",_board[_GDway[0].x][_GDway[0].y].id))&&Target(new GDposition(message.x,message.y),"enemy")&&CSgetState("可攻击",_board[_GDway[0].x][_GDway[0].y].id)){
					if(_board[message.x][message.y].team!=0){
					// 无目标
						if(_GDway.length==1){
							_GDway.push(new GDWay(message.x,message.y,_GDway.length));
							GUIShowAttackTarget();
						}
						// 有目标
						else if(_GDway.length>1){
							for(var j=1;j<_GDway.length;j++){
								if(_board[_GDway[j].x][_GDway[j].y] != null && _board[_GDway[j].x][_GDway[j].y].team != 0)
									g_GBTouchLayer.removeChild(_GUISprite);
								else
									_GDway[j].sprite.removeFromParent(true);
							} 
							_GDway.splice(1,_GDway.length-1);
							_GDway.push(new GDWay(message.x,message.y,_GDway.length));
							GUIShowAttackTarget();
						}
					}
				}
				// 目标为空格-或回到原点
				else if(_board[message.x][message.y]==null||GDPositionEqual(_GDway[0],message)){
					if(_board[_GDway[_GDway.length-1].x][_GDway[_GDway.length-1].y] != null && _board[_GDway[_GDway.length-1].x][_GDway[_GDway.length-1].y].team != 0){
						g_GBTouchLayer.removeChild(_GUISprite);
						_GDway.splice(1,_GDway.length-1);
					}
					var repete = false;
					// 遍历移动链，判断重复
					for(var temp in _GDway){
						if(_GDway[temp].x == message.x&&_GDway[temp].y == message.y){
							repete = true;
							for(var j=parseInt(temp);j<_GDway.length;j++){
									_GDway[j].sprite.removeFromParent(true);
							} 
							_GDway.splice(parseInt(temp)+1,_GDway.length-parseInt(temp)-1);
							_GDway[_GDway.length-1].sprite = new ControlSprite(_GDway[_GDway.length-1].no);
							g_GBTouchLayer.addChild(_GDway[_GDway.length-1].sprite);
							if(temp!=0){ 
								_GDway[_GDway.length-2].sprite.removeFromParent(true);
								_GDway[_GDway.length-2].sprite = new ControlSprite(_GDway[_GDway.length-2].no);
								g_GBTouchLayer.addChild(_GDway[_GDway.length-2].sprite);
								}
							break;
						}
					}
					// 非重复，建立新节点
					if(_board[_GDway[_GDway.length-1].x][_GDway[_GDway.length-1].y] == null || _board[_GDway[_GDway.length-1].x][_GDway[_GDway.length-1].y].team == 0)
						if(!repete&&_GDway.length <= CSgetState("移动力",_board[_GDway[0].x][_GDway[0].y].id)){
						if(Ring(_GDway[_GDway.length-1],new GDposition(message.x,message.y),1)){
							_GDway.push(new GDWay(message.x,message.y,_GDway.length));
							_GDway[_GDway.length-1].sprite = new ControlSprite(_GDway[_GDway.length-1].no);
							g_GBTouchLayer.addChild(_GDway[_GDway.length-1].sprite);
							_GDway[_GDway.length-2].sprite.removeFromParent(true);
							_GDway[_GDway.length-2].sprite = new ControlSprite(_GDway[_GDway.length-2].no);
							g_GBTouchLayer.addChild(_GDway[_GDway.length-2].sprite);
							}
						}
					}
				}
			}
		if(_GDMoveDone&&_GDNowSkill==null&&_GDway.length!=0){
				if(!(_GDway[_GDway.length-1].x == message.x&&_GDway[_GDway.length-1].y == message.y)){
				// 判断目标可攻击
				if(Range(new GDposition(_GDway[0].x,_GDway[0].y),new GDposition(message.x,message.y),CSgetState("射程",_board[_GDway[0].x][_GDway[0].y].id))&&Target(new GDposition(message.x,message.y),"enemy")&&CSgetState("可攻击",_board[_GDway[0].x][_GDway[0].y].id)){
					if(_board[message.x][message.y].team!=0){
						// 无目标
						if(_GDway.length==1){
							_GDway.push(new GDWay(message.x,message.y,_GDway.length));
							GUIShowAttackTarget();
						}
						// 有目标
						else if(_GDway.length>1){
							// 判断非重复;
							g_GBTouchLayer.removeChild(_GUISprite);
							_GDway.splice(1,_GDway.length-1);				
							_GDway.push(new GDWay(message.x,message.y,_GDway.length));
							GUIShowAttackTarget();
						}
					}
				}
				else{
					for(var j=1;j<_GDway.length;j++){
						if(_board[_GDway[j].x][_GDway[j].y] != null && _board[_GDway[j].x][_GDway[j].y].team != 0)
							g_GBTouchLayer.removeChild(_GUISprite);
						else
							_GDway[j].sprite.removeFromParent(true);
					} 
					_GDway.splice(1,_GDway.length-1);
				}
				}
			}
		}
	}
};

function GCUp(message){
	// 放开技能
	if(_GDNowSkill!=null){
		GUIResetSkillIcon();
		if(_GDNowSkillPosition!=null){
			g_GBTouchLayer.removeChild(_GUISprite);
			_GEEventList.push(new GEEvent("技能",_GDNowChess.id+","+_GDNowSkill+","+_GDNowSkillPosition.x+","+_GDNowSkillPosition.y));
			_GDAttackDone = true;
			GEAskForResponse();
		}
		else{
			GUIShowNowAction();
		}
		_GDNowSkillPosition = null;
		_GDNowSkill = null;
	}
	if(_GDway.length==1){
		_GDway[0].sprite.removeFromParent(true);
		GUIResetMenuIcon();
		GUIShowNowAction();
		GUIShowMenuIcon();
		_GDNowMenu = "选单";
	}
	else if(_GDway.length>1){
		if(_board[_GDway[_GDway.length-1].x][_GDway[_GDway.length-1].y] == null){
			for(var j=0;j<_GDway.length-1;j++){
				_GDway[j].sprite.removeFromParent(true);
				_GEEventList.push(new GEEvent("移动",_GDNowChess.id+","+_GDway[j].x+","+_GDway[j].y+","+_GDway[j+1].x+","+_GDway[j+1].y));
			}
			GEAskForResponse();
			_GDway[_GDway.length-1].sprite.removeFromParent(true);
			_GDMoveDone = true;
			_GDway = new Array();
		}
		else{
			g_GBTouchLayer.removeChild(_GUISprite);
			_GEEventList.push(new GEEvent("攻击",_GDNowChess.id+","+_board[_GDway[1].x][_GDway[1].y].id));
			_GDway[0].sprite.removeFromParent(true);
			_GDAttackDone = true;
			GEAskForResponse();
		}
	}
	_GDway = new Array();
};